package com.javabatch.petstore.fixtures;

import static fitnesse.util.ListUtility.list;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.javabatch.fileio.FieldError;
import com.javabatch.fileio.FileBuffer;
import com.javabatch.fileio.FileStore;
import com.javabatch.fileio.RecordBasedFileStore;
import com.javabatch.petstore.records.CatalogRecord;

/**
 * Simple file generator for use with fitnesse.
 *
 * @author eco
 */
public class CatalogFileFixture {
    /** Customer file. */
    private static FileStore store;


    /** order id. */
    private String id;

    /** Animal id. */
    private String animalId;

    /** desciption of animal. */
    private String description;

    /** Price of animal. */
    private String price;



    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }



    public String getAnimalId() {
        return animalId;
    }

    public void setAnimalId(String animalId) {
        this.animalId = animalId;
    }


    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    /** Constructor to use from test when closing file. */
    public CatalogFileFixture() {
    }

    public CatalogFileFixture(final String file, final String readWrite) {
        if (readWrite.equalsIgnoreCase("R")) {
            store = new RecordBasedFileStore(file,
                    RecordBasedFileStore.READ_ACCESS,
                    CatalogRecord.getLengthAndValidateRecord());

        } else {
            store = new RecordBasedFileStore(file,
                    RecordBasedFileStore.WRITE_ACCESS,
                    CatalogRecord.getLengthAndValidateRecord());
        }
        store.open();
    }


    /**
     * Write text to file.
     *
     * @return true if ok
     */
    public String add() {
        /* Should been in the constructor, but fitnesse
         * suppresses exceptions from construcutor */
   

        FileBuffer outputRecord = new FileBuffer();

        CatalogRecord.CATALOG_ID.write(outputRecord, this.id);
        CatalogRecord.ANIMAL_ID.write(outputRecord, this.animalId);
        CatalogRecord.DESCRIPTION.write(outputRecord, this.description);
        CatalogRecord.PRICE.write(outputRecord, this.price);

        store.write(outputRecord);
        return "";
    }

    public List<Object> query() {
        List<FieldError> errors = new ArrayList<FieldError>();
        List<Object> rows = new LinkedList<Object>();
        FileBuffer inputRecord = store.readNext();

        while (inputRecord != null) {
            List<Object> aRow =
                    list(list("id", CatalogRecord.CATALOG_ID.read(inputRecord, errors).replaceAll("^0*", "")),
                         list("animal id", CatalogRecord.ANIMAL_ID.read(inputRecord, errors).replaceAll("^0*", "")),
                         list("description", CatalogRecord.DESCRIPTION.read(inputRecord, errors).trim()),
                         list("price", CatalogRecord.PRICE.read(inputRecord, errors).replaceAll("^0*", "")));

            if (errors.size() == 0) {
                rows.add(aRow);
            } else {
                for (FieldError error : errors) {
                    rows.add(list(list("id", error.toString())));
                }
            }

            inputRecord = store.readNext();
        }

        return rows;
    }

    public static boolean close() {
        store.close();
        return true;
    }
}
